# 模型就放在这个里面

from exts import db
from datetime import datetime

# 用户表  ORM模型
class UserModel(db.Model):
    __tablename__ = "user"
    id = db.Column(db.Integer,primary_key=True, autoincrement=True)
    username = db.Column(db.String(100), nullable=False)
    password = db.Column(db.String(200), nullable=False)
    email = db.Column(db.String(100), nullable=False,unique=True)
    join_time = db.Column(db.DateTime, default=datetime.now)

# 邮件验证码的表   ORM模型
class EmailCaptchaModel(db.Model):
    __tablename__ = "email_captcha"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    email = db.Column(db.String(100), nullable=False)
    captcha = db.Column(db.String(100), nullable=False)
    # used = db.Column(db.Boolean,default=False)  #代表验证码是否已经被使用过


# 发布问答
class QuestionModel(db.Model):
    __tablename__ = "question"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    title = db.Column(db.String(100), nullable=False)
    content = db.Column(db.Text, nullable=False)
    create_time = db.Column(db.DateTime, default=datetime.now)

    # 这个问答到底是谁发布的，所以就需要外键来连接另一张表，连接user表的id
    # 外键
    author_id = db.Column(db.Integer, db.ForeignKey("user.id"))
    # 建立一对多或者一对一关系
    author = db.relationship(UserModel, backref="questions")


# 评论/回答的表
class AnswerModel(db.Model):
    __tablename__ = "answer"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    content = db.Column(db.Text, nullable=False)
    create_time = db.Column(db.DateTime, default=datetime.now)
    # 外键
    question_id = db.Column(db.Integer, db.ForeignKey("question.id"))
    author_id = db.Column(db.Integer, db.ForeignKey("user.id"))
    # 定义两个关系
    question = db.relationship(QuestionModel, backref=db.backref("answers", order_by=create_time.desc())) # 给评论排序，发表越晚的排序到前边
    author = db.relationship(UserModel, backref="answers")



























